#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# @Author  : tester
# @Site    : DONGFANG_HOTEL-10F
# @Software: PyCharm
# @datetime:2020/9/28 19:27
# @license: (C) Copyright 2018-2027, By 金锡科技.
"""
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from logger import getLoggers


class BasePage(object):
    def __init__(self, driver):
        self.driver = driver

    # 查找单个元素
    def find_element(self, *loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
            return self.driver.find_element(*loc)
        except:
           getLoggers.log.info(f'find element:没找到需点击的元素:{loc}')

    # 查找多个元素
    def find_elements(self, *loc):
        return self.driver.find_elements(*loc)

    # 单个元素输入信息
    def input_text(self, loc, text):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
            self.find_element(*loc).send_keys(text)
        except:
            getLoggers.log.info(f'input_text:没找到输入框元素:{loc}')

    # 获取多个相同元素，按照下标输入信息
    def list_input_text(self, loc, num, text):
        elements = self.find_elements(*loc)
        elements[num].send_keys(text)

    # 点击 控件
    def click(self, loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
            self.find_element(*loc).click()
        except:
            getLoggers.log.info(f'click:没找到需点击的元素:{loc}')

    # 执行键盘回车键
    def enter(self, loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
            self.find_element(*loc).send_keys(Keys.ENTER)
        except:
            getLoggers.log.info(f'enter:没找到需点击的元素:{loc}')

    # 获取控件的text信息
    def get_text(self, loc):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
        return self.find_element(*loc).text

    # 获取元素某个标签属性值
    def get_attribute_value(self, loc, tab):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
        return self.find_element(*loc).get_attribute(tab)

    # 进入iframe
    def switch_in_frame(self, loc):
        self.driver.switch_to.frame(loc)

    # 跳出当前iframe
    def switch_out_frame(self):
        self.driver.switch_to.parent_frame()

    # 返回最外层iframe
    def switch_default_frame(self):
        self.driver.switch_to.default_content()

    '''
	下拉框选择
	根据 text 下拉框的值 来选择
	'''

    def select(self, loc, text):
        Select(self.driver.find_element(*loc)).select_by_value(text)

    '''
	获取多个相同元素，按照下标选择下拉框
	根据 text 下拉框的值 来选择
	'''

    def select_all(self, loc, num, text):
        selects = self.find_elements(*loc)
        Select(selects[num]).select_by_value(text)

    # 清空控件信息
    def clear_input(self, loc):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
        self.find_element(*loc).clear()

    # 鼠标移入元素·
    def action_chain(self, loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
            element = self.find_element(*loc)
            ActionChains(self.driver).move_to_element(element).perform()
        except:
            getLoggers.log.info(f'鼠标移入没找到该元素:{loc}')

    # 查看元素是否存在
    def check_element_isexist(self, loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
            # EC.presence_of_element_located(loc)(self.driver)
            return True
        except:
            getLoggers.log.info(f'查找的元素不存在:{loc}')
            return False
